package com.dxy.df.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangsheng@dxy.cn
 * @date 2019-03-29
 **/
public class RegUtils {


    public static String formatRestfulUrlByKey(String url, String id,String patternKey ) {
        Pattern pattern = Pattern.compile("\\{"+patternKey+"\\}");
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            url = matcher.replaceAll(id);
        }
        return url;
    }


    public static String formatRestfulUrl(String url, Map<String, String> data) {
        if (null == data) {
            return url;
        }
        for (Map.Entry<String, String> entry : data.entrySet()) {
            Pattern pattern = Pattern.compile("\\{" + entry.getKey() + "\\}");
            Matcher matcher = pattern.matcher(url);
            if (matcher.find()) {
                url = matcher.replaceAll(entry.getValue());
            }
        }
        return url;
    }

    public static boolean match(String data, String pattern) {
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(data);
        return m.find();
    }

}
